﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SqlDataTypes
{
    public class SqlNumeric : SqlDataType
    {
        private string name = "numeric";
        private string sqlQuery;
        private const int MAXPRECISION = 38;
        private const int MINPRECISION= 1;
        private int precision;
        private int scale;

        public SqlNumeric()
        {
            precision = 18;
            scale = 0;
            sqlQuery = GetSqlQuery();
        }

        public SqlNumeric(int precision)
        {
            Precision = precision;
            scale = 0;
            sqlQuery = GetSqlQuery();
        }

        public SqlNumeric(int precision, int scale)
        {
            Precision = precision;
            Scale = scale;
            sqlQuery = GetSqlQuery();
        }

        private string GetSqlQuery()
        {
            return name + "(" + precision.ToString() +"," + scale.ToString() +  ")";
        }

        public override string Name
        {
            get { return name; }
        }

        public override string SqlCommand
        {
            get { return sqlQuery; }
        }

        private int Precision
        {
            set
            {
                if (value > MAXPRECISION)
                {
                    precision = MAXPRECISION;
                }
                else if (value < MINPRECISION)
                {
                    precision = MINPRECISION;
                }
                else
                {
                    precision = value;
                }
            }
        }

        private int Scale
        {
            set
            {
                if (value > precision)
                {
                    scale = precision;
                }
                else if (value < 0)
                {
                    scale = 0;
                }
                else
                {
                    scale = value;
                }
            }
        }
    }
}
